#! /usr/bin/env python3
# -*- coding: utf-8 -*-

import configparser


class ParamCache(object):
    def __init__(self, global_config: dict, unit_config: list):
        self.global_config = global_config
        self.units_config = unit_config
        self.save_info = {}
        # self.push_config()

    def add_save_info(self, save_info: dict):
        self.save_info.update(save_info)

    def get_save_info(self):
        return self.save_info

    def get_param_protocol(self):
        return self.config[1]['protocol']

    def get_param_coap_com(self):
        return self.global_config['coap']

    def get_param_serial_com(self):
        return self.global_config['serial']

    def get_param_request_com(self):
        return self.global_config['request']

    def get_param_config(self):
        return self.config[1]

    def push_config(self):
        self.config = self.units_config.pop(0)
        return self

    def config_count(self):
        return len(self.units_config)


if __name__ == '__main__':
    global_conf = unit_conf = configparser.ConfigParser()
    global_conf.read('./config.ini')
    unit_conf.read('./test01.ini')
    unit_list = sorted(unit_conf.items(), key=lambda x: x[0])
    unit_list = list(filter(lambda x: x[0] != 'DEFAULT', unit_list))

    p1 = ParamCache(global_conf, unit_list)

    coap = p1.get_param_coap_com()
    param = p1.get_param_config()
    param.update(coap)

    print(p1.get_param_protocol())
    for key, value in param.items():
        print(key + ':' + value)

    p2 = p1.push_config()
    print(p2.get_param_protocol())